/*
    This source file is part of GeNa project.
    Copyright © GeNa project 2008
    For the latest info, see http://code.google.com/p/genaproject/ or  http://lumpyproject.forums-rpg.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef MISC_H_INCLUDED
#define MISC_H_INCLUDED

namespace math
{

const unsigned long long int DeBruign6Seq = 0x43147259A7ABB7E0ll;
const unsigned int DeBruign6LookupTable[64] = { 63,62,61,56,60,50,55,44,59,38,49,35,54,29,43,23,58,46,37,25,48,17,34,15,53,32,28,9,42,13,22,6,0,57,51,45,39,36,30,24,47,26,18,16,33,10,14,7,1,52,40,31,27,19,11,8,2,41,20,12,3,21,4,5 };

const unsigned long int DeBruign5Seq = 0x4653ADF;
const unsigned int DeBruign5LookupTable[32] = { 0,1,2,6,3,11,7,16,4,14,12,21,8,23,17,26,31,5,10,15,13,20,22,25,30,9,19,24,29,18,28,27 };

inline unsigned int log2( unsigned int v)
{
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    v = (v >> 1) + 1;

    return DeBruign5LookupTable[(uint32_t)(v * DeBruign5Seq) >> 27];
}

} //namespace math

#endif // MISC_H_INCLUDED
